<template>
  <div class="address-list">
    <div class="title">{{title}}</div>
    <div class="list">
      <div class="list-item" v-for="(item, index) in list" :key="index" @click="clickItem(item, index)">
        <div class="name">
          {{item[labelKey]}}
        </div>
<!--        <div> - </div>-->
        <div>
          <el-checkbox
              :model-value="item.exist === 1"
              @change="(val) => change(val, item, index)"
              :indeterminate="checkIndeterminate(item)">
          </el-checkbox>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
// import {IAddressRow} from "../types/deliverySetting";
import {IDeliverItem} from "@/api/modules/pointsMall";

const {title, list, labelKey, valueKey} = defineProps<{
  title: string,
  labelKey: string,
  valueKey: string,
  list: IDeliverItem[]
}>()

const emits = defineEmits(['click-item', 'click-checkbox'])

const checkIndeterminate = (item: IDeliverItem) => {
  const all = item.children ?? []
  const checked = all.filter(cItem => cItem.exist)

  return checked.length !== all.length && checked.length !== 0
}

const clickItem = (item: IDeliverItem, index: number) => {
  emits('click-item', item, index)
}

const change = (val: boolean, item: IDeliverItem, index: number) => {
  emits('click-checkbox', val ? 1 : 0, item, index)
}


</script>

<style scoped lang="scss">
.address-list {
  width: 300px;
  margin-right: 20px;
  position: relative;
}
.title {
  position: sticky;
  top: 0;
  width: 100%;
  padding: 5px 20px;
  z-index: 2;
  background: white;
  box-sizing: border-box;
}
.list{
  height:600px;
  overflow-y: auto;
}
.list-item {
  padding: 5px 20px;
  cursor: pointer;
  margin: 2px 0 0;
  background: #F0F0F0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;

}
.list-item:hover {
  background: #F2F2F2;
}
</style>
